import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Layout from '@/views/layout/Layout'
import {getMenuList} from "@/api/menu"
import {CODE_OK,TAG_DEFAULT_COLOR,TAG_ACTIVE_COLOR} from "@/common/js/config"
import {isURL} from '@/common/js/validate'

Vue.use(Router)

//全局路由，无需嵌套
const globalRoutes = [
	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/common/login'),
		meta: {
		  title: '登录'
		}
 	},	{
		path: '/dataview',
		name: 'dataview',
		component: () => import('@/views/modules/tables/dataview'),
		meta: {
		  title: '大屏'
		}
 	}
]

//主路由，需要嵌套
const mainRoutes = {
	path: '/',
	name: 'Layout',
	redirect: "/home",
	component: Layout,
	children: [
		{
			path: '/home',
	 		name: '首页',
	 		component: () => import('@/views/modules/tables/dataview'),
	 		meta: {
				title: '首页',
        componentName: 'home'
			}
		},
    //=========================
    {
    	path: '/registcheck1-02',
    	name: '原料请验单02版',
    	component: () => import('@/views/modules/process/registcheck/registcheck1/version02'),
    	meta: {
    	  title: '原料请验单02版',
        componentName: 'registcheck102',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck1-03',
    	name: '原料请验单03版',
    	component: () => import('@/views/modules/process/registcheck/registcheck1/version03'),
    	meta: {
    	  title: '原料请验单03版',
        componentName: 'registcheck103',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck1-04',
    	name: '原料请验单04版',
    	component: () => import('@/views/modules/process/registcheck/registcheck1/version04'),
    	meta: {
    	  title: '原料请验单04版',
        componentName: 'registcheck104',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck2-02',
    	name: '辅料请验单02版',
    	component: () => import('@/views/modules/process/registcheck/registcheck2/version02'),
    	meta: {
    	  title: '辅料请验单02版',
        componentName: 'registcheck202',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck2-03',
    	name: '辅料请验单03版',
    	component: () => import('@/views/modules/process/registcheck/registcheck2/version03'),
    	meta: {
    	  title: '辅料请验单03版',
        componentName: 'registcheck203',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck2-04',
    	name: '辅料请验单04版',
    	component: () => import('@/views/modules/process/registcheck/registcheck2/version04'),
    	meta: {
    	  title: '辅料请验单04版',
        componentName: 'registcheck204',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck3-02',
    	name: '包材请验单02版',
    	component: () => import('@/views/modules/process/registcheck/registcheck3/version02'),
    	meta: {
    	  title: '包材请验单02版',
        componentName: 'registcheck302',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck3-03',
    	name: '包材请验单03版',
    	component: () => import('@/views/modules/process/registcheck/registcheck3/version03'),
    	meta: {
    	  title: '包材请验单03版',
        componentName: 'registcheck303',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck3-04',
    	name: '包材请验单04版',
    	component: () => import('@/views/modules/process/registcheck/registcheck3/version04'),
    	meta: {
    	  title: '包材请验单04版',
        componentName: 'registcheck304',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck4-00',
    	name: '中间品请验单00版',
    	component: () => import('@/views/modules/process/registcheck/registcheck4/version00'),
    	meta: {
    	  title: '中间品请验单00版',
        componentName: 'registcheck400',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck4-01',
    	name: '中间品请验单01版',
    	component: () => import('@/views/modules/process/registcheck/registcheck4/version01'),
    	meta: {
    	  title: '中间品请验单01版',
        componentName: 'registcheck401',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck5-00',
    	name: '成品请验单00版',
    	component: () => import('@/views/modules/process/registcheck/registcheck5/version00'),
    	meta: {
    	  title: '成品请验单00版',
        componentName: 'registcheck500',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck5-01',
    	name: '成品请验单01版',
    	component: () => import('@/views/modules/process/registcheck/registcheck5/version01'),
    	meta: {
    	  title: '成品请验单01版',
        componentName: 'registcheck501',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck6-00',
    	name: '稳定性考察请验单00版',
    	component: () => import('@/views/modules/process/registcheck/registcheck6/version00'),
    	meta: {
    	  title: '稳定性考察请验单00版',
        componentName: 'registcheck600',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck6-01',
    	name: '稳定性考察请验单01版',
    	component: () => import('@/views/modules/process/registcheck/registcheck6/version01'),
    	meta: {
    	  title: '稳定性考察请验单01版',
        componentName: 'registcheck601',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck7-03',
    	name: '制药用水/纯蒸汽请验单04版',
      // name: '制药用水/纯蒸汽请验单03版',
    	component: () => import('@/views/modules/process/registcheck/registcheck7/version03'),
    	meta: {
    	  title: '制药用水/纯蒸汽请验单04版',
        // title: '制药用水/纯蒸汽请验单03版',
        componentName: 'registcheck703',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck8-00',
    	name: '环境监测请验单',
    	component: () => import('@/views/modules/process/registcheck/registcheck8/version00'),
    	meta: {
    	  title: '环境监测请验单',
        componentName: 'registcheck800',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck9-02',
    	name: '其他请验单02版',
    	component: () => import('@/views/modules/process/registcheck/registcheck9/version02'),
    	meta: {
    	  title: '其他请验单02版',
        componentName: 'registcheck902',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck9-03',
    	name: '其他请验单03版',
    	component: () => import('@/views/modules/process/registcheck/registcheck9/version03'),
    	meta: {
    	  title: '其他请验单03版',
        componentName: 'registcheck903',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck9-04',
    	name: '其他请验单04版',
    	component: () => import('@/views/modules/process/registcheck/registcheck9/version04'),
    	meta: {
    	  title: '其他请验单04版',
        componentName: 'registcheck904',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck10-01',
    	name: '独立请验01版',
    	component: () => import('@/views/modules/process/registcheck/registcheck10/version01'),
    	meta: {
    	  title: '独立请验01版',
        componentName: 'registcheck101',
        keepAlive: false
    	}
    },
    {
    	path: '/registcheck11-00',
    	name: '待办请验单',
    	component: () => import('@/views/modules/process/registcheck/registcheck11/version00'),
    	meta: {
    	  title: '待办请验单',
        componentName: 'registcheck1100',
        keepAlive: false
    	}
    }
	],
	beforeEnter(to, from, next) {
    if (store.getters.userInfo || to.name == "dataview") {
      next()
    } else {
      next({
        path: '/login'
      })
    }
	}
}

const router = new Router({
	mode: 'history',
	scrollBehavior: () => ({
    y: 0
  }),
  isAddDynamicMenuRoutes: false,
	routes: globalRoutes.concat(mainRoutes)
})

router.beforeEach((to, from, next) => {
	NProgress.start()
	if (to.name == "login"  || to.name == "dataview" || router.options.isAddDynamicMenuRoutes){
		if (router.options.isAddDynamicMenuRoutes) {
			let isAdd = true
			for (let i=0,j=store.getters.mainTagsActiveName.length; i<j; i++) {
				store.getters.mainTagsActiveName[i].color = TAG_DEFAULT_COLOR
				if (to.name == store.getters.mainTagsActiveName[i].name) {
					isAdd = false
					store.getters.mainTagsActiveName[i].color = TAG_ACTIVE_COLOR
				}
			}
			if (isAdd) {
				let tag = {
		    	name: to.name,
		    	path: to.path,
		    	color: TAG_ACTIVE_COLOR
				}
				store.getters.mainTagsActiveName.push(tag)
			}
      if (!to.meta.keepAlive) {
        store.commit('ADD_NO_KEEP_ALIVE', to.meta.componentName)
      }
		}
		next()
	} else {
		getMenuList().then(res => {
			if (res && res.code === CODE_OK) {
				router.options.isAddDynamicMenuRoutes = true
				let menuList = res.menuList
				addDynamicMenuRoutes(menuList)
				store.commit('menuList', res.menuList)
				store.commit('updateUserInfo', res.user)
				store.commit('permissions', res.permissions)
				next({
      		...to,
      		replace: true
      	})
			} else {
				store.commit('menuList', [])
				store.commit('updateUserInfo', [])
				store.commit('permissions', [])
				next({
      		path: '/login'
    		})
			}
		}).catch(err => {
  		console.log(err)
    })
	}
})

router.afterEach(() => {
  NProgress.done()
})

function addDynamicMenuRoutes (menuList = [], routes = []) {
	let temp = []
	for (let i=0; i<menuList.length; i++){
		if (menuList[i].childList && menuList[i].childList.length >= 1){
			temp = temp.concat(menuList[i].childList)
		} else if (/\S/.test(menuList[i].url) && menuList[i].url) {
			let route = {
				path: menuList[i].url,
				name: menuList[i].menuName,
				component: null,
				meta: {
          menuId: menuList[i].menuId,
          title: menuList[i].menuName,
          isDynamic: true,
          isTab: true,
          iframeUrl: menuList[i].url,
          isIframe: menuList[i].isIframe,
          keepAlive: true
        }
			}
      if (menuList[i].isIframe == 0 && isURL(menuList[i].url)) {
        route.component = resolve => resolve({
          template:
            `<div class="app-container" style="height: calc(100vh - 134px)">
              <iframe src="`+menuList[i].url+`" frameborder="0" width="100%" style="height: calc(100vh - 134px)" />
            </div>`
        })
        route['path'] = `i-${menuList[i].menuId}`
      }else{
        try {
          route.component = () => import(`@/views/modules/${menuList[i].url}`) || null
        } catch (e) {
          route.component = resolve => resolve({
            template:
              `<h1>模块未找到</h1>`
          })
        }
      }
			routes.push(route)
		}
	}
	if (temp.length >= 1) {
		addDynamicMenuRoutes(temp, routes)
	} else {
		mainRoutes.children = routes
		router.addRoutes([
      mainRoutes
    ])
	}
}

export default router
